package cn.cloudheritageservice8002.controller;

import cn.cloudheritageservice8002.entity.CulturalHeritage;
import cn.cloudheritageservice8002.service.CulturalHeritageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/feign/heritage")
public class CulturalHeritageController {
    
    @GetMapping("/test")
    public ResponseEntity<String> test() {
        return ResponseEntity.ok("Test endpoint is working!");
    }
    
    private static final Logger logger = LoggerFactory.getLogger(CulturalHeritageController.class);
    
    @Autowired
    private CulturalHeritageService heritageService;

    @GetMapping
    public ResponseEntity<List<CulturalHeritage>> listHeritage() {
        logger.info("收到获取文化遗产列表请求");
        List<CulturalHeritage> heritageList = heritageService.listHeritage();
        logger.info("返回文化遗产列表，数量: {}", heritageList != null ? heritageList.size() : 0);
        return ResponseEntity.ok(heritageList);
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<CulturalHeritage> getHeritageById(@PathVariable("id") Long id) {
        try {
            logger.info("收到获取文化遗产请求，ID: {}", id);
            if (id == null) {
                logger.error("ID不能为null");
                return ResponseEntity.badRequest().build();
            }
            logger.debug("调用heritageService.getHeritageById({})", id);
            CulturalHeritage heritage = heritageService.getHeritageById(id);
            if (heritage == null) {
                logger.warn("未找到ID为 {} 的文化遗产", id);
                return ResponseEntity.notFound().build();
            }
            logger.info("成功获取ID为 {} 的文化遗产: {}", id, heritage);
            return ResponseEntity.ok(heritage);
        } catch (Exception e) {
            logger.error("获取文化遗产详情时发生异常，ID: " + id, e);
            return ResponseEntity.status(500).build();
        }
    }

    @GetMapping("/search")
    public ResponseEntity<List<CulturalHeritage>> searchHeritage(
            @RequestParam(required = false) String keyword) {
        
        if (keyword == null || keyword.trim().isEmpty()) {
            logger.info("收到空搜索请求，返回所有记录");
            return listHeritage();
        }
        
        logger.info("收到搜索文化遗产请求，关键词: {}", keyword);
        try {
            List<CulturalHeritage> result = heritageService.search(keyword.trim());
            logger.info("搜索到 {} 条记录", result != null ? result.size() : 0);
            if (result != null && !result.isEmpty()) {
                logger.debug("搜索结果为: {}", result);
            } else {
                logger.warn("未找到与'{}'相关的文化遗产", keyword);
            }
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            logger.error("搜索文化遗产时发生异常，关键词: " + keyword, e);
            return ResponseEntity.status(500).build();
        }
    }

    @PostMapping
    public ResponseEntity<CulturalHeritage> createHeritage(@RequestBody CulturalHeritage heritage) {
        logger.info("收到创建文化遗产请求: {}", heritage);
        CulturalHeritage savedHeritage = heritageService.createHeritage(heritage);
        logger.info("创建文化遗产成功，ID: {}", savedHeritage != null ? savedHeritage.getId() : "null");
        return ResponseEntity.ok(savedHeritage);
    }

    @PutMapping("/{id}")
    public ResponseEntity<CulturalHeritage> updateHeritage(
            @PathVariable("id") Long id, 
            @RequestBody CulturalHeritage heritage) {
        logger.info("收到更新文化遗产请求，ID: {}, 数据: {}", id, heritage);
        heritage.setId(id);
        CulturalHeritage updatedHeritage = heritageService.updateHeritage(heritage);
        logger.info("更新文化遗产成功，ID: {}", id);
        return ResponseEntity.ok(updatedHeritage);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> deleteHeritage(@PathVariable("id") Long id) {
        logger.info("收到删除文化遗产请求，ID: {}", id);
        boolean result = heritageService.deleteHeritage(id);
        logger.info("删除文化遗产{}，ID: {}", result ? "成功" : "失败", id);
        return ResponseEntity.ok(result);
    }
}
